using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KissFish : Fish
{
    [Header("移动系统")]
    private Vector3 moveDirection;   // 鱼的移动方向
    private float screenWidth;       // 屏幕宽度（世界单位）
    private float leftBoundary;      // 左边界位置
    private float rightBoundary;     // 右边界位置


    protected override void InitializeMovement()
    {
        // 随机决定鱼是向左还是向右移动
        bool moveRight = Random.value > 0.5f;

        // 设置移动方向和精灵朝向
        moveDirection = moveRight ? Vector3.right : Vector3.left;
        _spriteRenderer.flipX = !moveRight;
    }

    protected override void Move()
    {
        // 水平直线移动
        transform.Translate(moveDirection * fishMoveSpeed * Time.deltaTime);
    }

    protected override void CalculateBounds()
    {
        screenWidth = 2f * mainCamera.orthographicSize * mainCamera.aspect;
        float extendDistance = 1.0f; // 边界扩展距离，可调整

        Vector3 cameraPosition = mainCamera.transform.position;
        leftBoundary = cameraPosition.x - screenWidth * 0.5f - extendDistance;
        rightBoundary = cameraPosition.x + screenWidth * 0.5f + extendDistance;
    }

    protected override void CheckBounds()
    {
        Vector3 position = transform.position;

        // 仅当鱼向左移动时检测左边界
        if (moveDirection.x < 0 && position.x <= leftBoundary)
        {
            ReverseDirection();
        }

        // 仅当鱼向右移动时检测右边界
        if (moveDirection.x > 0 && position.x >= rightBoundary)
        {
            ReverseDirection();
        }
    }

    protected override void ReverseDirection()
    {
        // 确保鱼不会超出边界太多
        Vector3 position = transform.position;
        if (moveDirection.x < 0) // 向左移动碰到左边界
            position.x = leftBoundary;
        else                   // 向右移动碰到右边界
            position.x = rightBoundary;

        transform.position = position;

        // 反转移动方向
        moveDirection *= -1;
        _spriteRenderer.flipX = !_spriteRenderer.flipX;
    }
}
